package com.jt.tenant.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.amap.api.location.AMapLocation;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.Tip;
import com.amap.api.services.route.BusRouteResult;
import com.amap.api.services.route.DrivePath;
import com.amap.api.services.route.DriveRouteResult;
import com.amap.api.services.route.RideRouteResult;
import com.amap.api.services.route.RouteSearch;
import com.amap.api.services.route.WalkRouteResult;
import com.jt.tenant.R;
import com.jt.tenant.action.LocationModular;
import com.jt.tenant.adapter.NavigationAddressAdapter;
import com.jt.tenant.app.JTApplication;
import com.jt.tenant.widget.SearchEditView;
import com.zcx.helper.http.Http;
import com.zcx.helper.util.UtilToast;

import java.util.ArrayList;
import java.util.List;

import overlay.DrivingRouteOverlay;

/**
 * Created by Administrator on 2016/4/22.
 */
public class NavigationAddressActivity extends JTActivity implements View.OnClickListener {

    private TextView title_name;

    private MapView mapView;

    private AMap aMap;

    private SearchEditView et_content;

    private ImageView iv_clear;

    private TextView tv_search;

    private ListView listView;

    private List<NavigationAddressAdapter.Info> search = new ArrayList<>();

    private RouteSearch routeSearch;

    private Inputtips inputtips;

    private NavigationAddressAdapter navigationAddressAdapter;

    private double mLat, mLon;

    private NavigationAddressAdapter.Info currentInfo;

    private AMapLocation currentAMapLocation;

    private LocationModular.OnLocateionChangeCallBack onLocateionChangeCallBack = new LocationModular.OnLocateionChangeCallBack(){

        @Override
        public void onLocation(AMapLocation aMapLocation) {

            currentAMapLocation = aMapLocation;

            LatLng latLng = new LatLng(mLat = aMapLocation.getLatitude(), mLon = aMapLocation.getLongitude());

            Log.e("LOCATION",mLat+"    "+mLon);

            aMap.clear();

            aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 13));

            aMap.addMarker(new MarkerOptions().position(latLng).draggable(false).icon(BitmapDescriptorFactory.fromResource(R.mipmap.icon_car_location)));

        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_navigation_address);

        (title_name = (TextView) findViewById(R.id.tv_title_name)).setText("导航");

        (iv_clear = (ImageView) findViewById(R.id.address_iv_clear)).setOnClickListener(this);

        (tv_search = (TextView) findViewById(R.id.address_tv_search)).setOnClickListener(this);

        listView = (ListView) findViewById(R.id.address_listview);

        mapView = (MapView) findViewById(R.id.address_mapview);

        mapView.onCreate(savedInstanceState);

        aMap = mapView.getMap();

        (et_content = (SearchEditView) findViewById(R.id.address_et_content)).setOnChangeCallBack(new SearchEditView.OnChangeCallBack() {

            @Override
            public void onChange(String text) {

                try {

                    listView.setVisibility(View.VISIBLE);

                    mapView.setVisibility(View.GONE);

                    inputtips.requestInputtips(text, currentAMapLocation.getCityCode());

                } catch (Exception e) {

                    e.printStackTrace();

                }

            }

        });

        (routeSearch = new RouteSearch(this)).setRouteSearchListener(new RouteSearch.OnRouteSearchListener() {

            //车辆新路规划数据
            @Override
            public void onDriveRouteSearched(DriveRouteResult driveRouteResult, int rCode) {

                if (rCode == 1000 && driveRouteResult != null && driveRouteResult.getPaths() != null && driveRouteResult.getPaths().size() > 0) {

                    aMap.clear();

                    DrivePath drivePath = driveRouteResult.getPaths().get(0);

                    DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(context, aMap, drivePath, driveRouteResult.getStartPos(), driveRouteResult.getTargetPos(),null);

                    drivingRouteOverlay.removeFromMap();

                    drivingRouteOverlay.addToMap();

                    drivingRouteOverlay.zoomToSpan();

                    search.clear();

                    et_content.setText(currentInfo.title);

                    listView.setVisibility(View.GONE);

                    mapView.setVisibility(View.VISIBLE);

                } else {

                    UtilToast.show(context, "线路规划失败");

                }

                Http.getInstance().dismiss();

            }

            @Override
            public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {
            }


            @Override
            public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {
            }

            @Override
            public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {

            }

        });

        inputtips = new Inputtips(this, new Inputtips.InputtipsListener() {

            @Override
            public void onGetInputtips(List<Tip> list, int k) {

                search.clear();

                for (int i = 0; i < list.size(); i++) {

                    Tip tip = list.get(i);

                    if(tip.getPoint() != null){

                        NavigationAddressAdapter.Info info = new NavigationAddressAdapter.Info();

                        info.title = tip.getName();

                        info.content = tip.getDistrict();

                        info.latLonPoint = tip.getPoint();

                        search.add(info);

                    }

                }

                navigationAddressAdapter = new NavigationAddressAdapter(context, search);

                listView.setAdapter(navigationAddressAdapter);

                navigationAddressAdapter.notifyDataSetChanged();

                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        Http.getInstance().show(context);

                        currentInfo = search.get(position);

                        routeSearch.calculateDriveRouteAsyn(new RouteSearch.DriveRouteQuery(new RouteSearch.FromAndTo(new LatLonPoint(mLat, mLon), new LatLonPoint(currentInfo.latLonPoint.getLatitude(), currentInfo.latLonPoint.getLongitude())), RouteSearch.DrivingDefault, null, null, ""));

                    }

                });

            }

        });

        JTApplication.locationModular.addOnLocateionChangeCallBack(onLocateionChangeCallBack);

    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {

            case R.id.address_iv_clear:

                et_content.setText("");

                search.clear();

                listView.setVisibility(View.GONE);

                mapView.setVisibility(View.VISIBLE);

                break;

            case R.id.address_tv_search:

                if (currentInfo == null) {

                    UtilToast.show(context, "请检索地址规划新路");

                } else {

                    startActivity(new Intent(context, BasicNaviActivity.class)
                            .putExtra("mLat", mLat)
                            .putExtra("mLon", mLon)
                            .putExtra("goLat", currentInfo.latLonPoint.getLatitude())
                            .putExtra("goLon", currentInfo.latLonPoint.getLongitude()));

                }

                break;

        }

    }

    @Override
    protected void onPause() {

        super.onPause();

        mapView.onPause();

    }

    @Override
    protected void onDestroy() {

        super.onDestroy();

        mapView.onDestroy();

        JTApplication.locationModular.removeOnLocateionChangeCallBack(onLocateionChangeCallBack);

    }

}
